package LeetCode;

public class LC_024_SwapNodesInPairs {
    public static void main(String[] args) {

    }

    private ListNode swapPairs(ListNode head) {
        ListNode root = new ListNode(-1);
        root.next = head;
        ListNode pre_head = root;

        while (head != null && head.next != null) {
            ListNode Node1 = head;
            ListNode Node2 = head.next.next;
            root.next = head.next;
            root = root.next;
            root.next = Node1;
            Node1.next = Node2;
            root = root.next;
            head = Node2;
        }

        return pre_head.next;
    }

    public ListNode swapPairs_2(ListNode head) {
        if (head == null)
            return null;

        if (head.next == null)
            return head;

        ListNode temp = head.next;
        head.next = swapPairs(temp.next);
        temp.next = head;
        return temp;
    }
}
